﻿using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameController : MonoBehaviour
{
    public enum GameState
    {
        PREPARE,
        FLY,
        REWARD,
    }

    public static GameController instance;
    public GameObject rocket, camera;
    public GameObject uiFly, uiSit;
    public Transform positionSit, positionFly;
    public Object power, speedup, w, zoc1, zoc2, blackYun;
    List<Object> allZoc = new List<Object>();
    public Object yun;
    public Object citie;
    public bool isCitie;
    public List<GameObject> toClear = new List<GameObject>();

    private void Awake()
    {
        instance = this;
        Application.targetFrameRate = 60;
        
    }

    // Start is called before the first frame update
    void Start()
    {
        allZoc = new List<Object>() { power, speedup, w, zoc1, zoc2, power, w, blackYun };
        StartCoroutine(IECreateObstacle());
        MessageCenter.instance.AddEvtListener(MsgType.citie, GetNewCitie);
    }

    public int currentLv = 0;
    IEnumerator IECreateObstacle()
    {
        while (true)
        {
            int hight = Mathf.CeilToInt(rocket.transform.position.y / 50.00f);
            if (hight > currentLv)
            {
                for (int i = 0; i < 45; i++)
                {
                    float x = Random.Range(rocket.transform.position.x - 280.0f, rocket.transform.position.x + 280.0f);
                    float y = Random.Range(hight * 50.0f, (hight + 1) * 50.0f);
                    int index = Random.Range(0, allZoc.Count);
                    GameObject temp = Instantiate(allZoc[index], new Vector3(x, y, 0), Quaternion.identity) as GameObject;
                    toClear.Add(temp);
                }
                for (int i = 0; i < 15; i++)
                {
                    float x = Random.Range(rocket.transform.position.x - 280.0f, rocket.transform.position.x + 280.0f);
                    float y = Random.Range(hight * 50.0f, (hight + 1) * 50.0f);
                    float z = Random.Range(-10f, -70f);
                    GameObject goYun = Instantiate(yun, new Vector3(x, y, z), Quaternion.Euler(0, 180, 0)) as GameObject;
                    float scale = Random.Range(8f, 24f);
                    goYun.transform.localScale = new Vector3(scale, scale, scale);
                    toClear.Add(goYun);
                }
                for (int i = 0; i < 5; i++)
                {
                    float x = Random.Range(rocket.transform.position.x - 280.0f, rocket.transform.position.x + 280.0f);
                    float y = Random.Range(hight * 50.0f, (hight + 1) * 50.0f);
                    GameObject goCitie = Instantiate(citie, new Vector3(x, y, 0), Quaternion.Euler(0, 180, 0)) as GameObject;
                    toClear.Add(goCitie);
                }
                currentLv = hight;
            }
            yield return new WaitForSeconds(2);
        }
    }

    public void ResetCamera()
    {
        camera.GetComponent<follow>().canFollow = false;
        uiFly.SetActive(false);
        uiSit.SetActive(true);
        uiSit.GetComponent<UIPrepare>().Reset();
    }

    void GetNewCitie(CustomMessage m)
    {
        if (iect != null)
            StopCoroutine(iect);
        iect = StartCoroutine(IEGetNewCitie());
    }

    Coroutine iect;
    IEnumerator IEGetNewCitie()
    {
        isCitie = true;
        yield return new WaitForSeconds(10f);
        isCitie = false;
        CustomMessage m = new CustomMessage(MsgType.noCitie);
        m.send();
    }

    private void OnDestroy()
    {
        MessageCenter.instance.RemoveEvtListener(MsgType.citie, GetNewCitie);
    }
}